package com.cgs.query.monitor.metrics

import com.codahale.metrics.Meter
import com.codahale.metrics.MetricRegistry

enum class CodahaleMetricsHelper {

    INSTANCE;

    val registry: MetricRegistry = MetricRegistry()

    /**
     * 获取系统的流速度量
     */
    fun getServerMeterInfo(): List<MeterInfo> {
        return registry.getMeters { name, _ ->
            name == "server"
        }.map {
            meter2Info(it.value)
        }
    }


    /**
     * 获取某个项目的流速度量
     */
    fun getProjectMeterInfo(projectName: String): List<MeterInfo> {
        return registry.getMeters { name, _ ->
            name == "server.$projectName"
        }.map {
            meter2Info(it.value)
        }
    }


    fun meter2Info(meter: Meter): MeterInfo {
        return MeterInfo(
                meter.count,
                meter.fifteenMinuteRate,
                meter.fiveMinuteRate,
                meter.oneMinuteRate,
                meter.meanRate
        )
    }
}

data class MeterInfo(
        val count: Long,
        val fifteenMinuteRate: Double,
        val fiveMinuteRate: Double,
        val oneMinuteRate: Double,
        val meanRate: Double
)
